import { Command } from 'commander'
import { GitStatus } from './git/git-status'
import { br } from './branches'
import { git } from './git/git'
import { cmContent } from './cm-content'
import { cmX } from './cm-x'
import open from 'open'
import path from 'path'
import { cmMerge } from './cm-merge'
import { target } from './target'
import { autoPush, autoUpdateCommit } from './auto-update'
import { cmList } from './cm-list'

const program = new Command()

program //
  .command('t')
  .description('创建临时分支')
  .argument('[targetBranch]', '分支名字')
  .action((target: string) => {
    if (target.match(/^\w+$/)) {
      const status = new GitStatus()
      if (status.hasChanged()) {
        console.log(
          'Some changed not commited yet! Reset Checkout will make change lost!',
        )
        return
      }
      br.checkoutBranch(`temp/${target}`)
      git.resetHard(target)
    }
  })

program //
  .command('mr')
  .description('提交 MR 到 gitlab上')
  .argument('[targetBranch]', '目标分支名字')
  .action((target: string) => {
    br.mergeRequest(target)
  })

program //
  .command('dl')
  .description('删除某个分支')
  .argument('[targetBranch]', '目标分支名字')
  .option('--fixed', '删除所有已经修复的分支，大概是一个月之前的。')
  .option('--temp', '删除所有临时的分支')
  .action((target: string, opt: { fixed: boolean; temp: boolean }) => {
    if (opt.fixed) {
      br.removeOldBranches('fix', 1000 * 60 * 60 * 24 * 20)
    } else if (opt.temp) {
      br.removeOldBranches('temp')
    } else {
      br.removeBranch(target)
    }
  })

program//
  .command('ls')
  .description('列出所有分支')
  .action(() => {
    br.listBranches()
  })

program //
  .command('rebase')
  .argument('[targetBranch]', '目标分支, 如果没有将会自动寻找自己的父亲分支')
  .description('更新数据, 并且默认使用 rebase 同步自己的父分支')
  .action(() => {
    br.rebaseParentBranch()
  })

program //
  .command('up')
  .description('更新版本号')
  .argument('[commit...]', '提交的内容')
  .option('-p, --push', '是否推送')
  .action((commit: string[], opt: { push: boolean }) => {
    autoUpdateCommit(commit.join(' '), opt.push)
  })

program // 提交代码, 但是会先 fetch, rebase 的方式同步一次远端的仓库
  .command('p')
  .description('提交代码, 包括对子模块的代码提交')
  .action(() => {
    autoPush()
  })

program //
  .command('MR')
  .description('将当前分支合并到目标分支')
  .argument('[targetBranch]', '目标分支')
  .action((target?: string) => {
    // 'https://gitea.gz4399.com/4399OM/InfofeAppIm/compare/2502d1/%e5%91%a8%e7%89%88%e6%9c%ac%e5%88%86%e6%94%af...2502d1/ui-bugfix'
    // 'https://gitea.gz4399.com/4399OM/InfofeAppIm/compare/2502d1/%E5%91%A8%E7%89%88%E6%9C%AC%E8%BF%AD%E4%BB%A3...2502d1/ui-bugfix'
    const project = path.basename(process.cwd())
    console.log('project: ', project)
    const mergeBaseURL = `https://gitea.gz4399.com/4399OM/${project}/compare`
    const bindMergeBranch = (from: string, to: string) => {
      return `${mergeBaseURL}/${to}...${from}`
    }
    const currentBranch = br.currentBranch
    const dir = currentBranch.split('/')[0]
    const targetBranch = target || (dir + '/周版本分支')
    open(bindMergeBranch(currentBranch, targetBranch))
  })

cmContent(program)
cmMerge(program, 'md', target.dev)
cmMerge(program, 'mw', target.week)
cmMerge(program, 'mm', target.master)
cmMerge(program, 'merge-dev', target.dev)
cmMerge(program, 'merge-week', target.week)
cmMerge(program, 'merge-master', target.master)
cmX(program)
cmList(program)

try {
  program.parse()
} catch (err) {
  if (err instanceof Error) {
    console.log(err.message)
  } else {
    console.log(err)
  }
}
